package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.common.CreateUUID;
import com.demo.model.TabMoneyShops;
import com.demo.service.TabMoneyShopsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@Log4j2
public class TabMoneyShopsController {

    @Resource
    private TabMoneyShopsService service;

    @RequestMapping(value = "tab_money_shops_view")
    public String tab_money_shops_view(String msname, String discription, String money, HttpServletRequest request){
        if(null==msname){
            msname = "";
        }
        if(null==discription){
            discription = "";
        }
        if(null==money){
            money = "";
        }
        TabMoneyShops shop = new TabMoneyShops();
        shop.setMsname(msname);
        shop.setDiscription(discription);
        shop.setMoney(money);
        service.searchPages(shop,request);
        request.setAttribute("msname",msname);
        request.setAttribute("discription",discription);
        request.setAttribute("money",money);
        return "index/tab_money_shops/view";
    }

    @RequestMapping(value = "tab_money_shops_pre_save")
    public String tab_money_shops_pre_save(){
        return "index/tab_money_shops/save";
    }

    @RequestMapping(value = "tab_money_shops_save")
    public String tab_money_shops_save(TabMoneyShops shop, MultipartFile file, HttpServletRequest request) throws IOException {
        String id = CreateUUID.getuuid();
        String loginname = (String) request.getSession().getAttribute("loginname");
        String datetimes = CreateDate.getDateTimes();


        //处理流业务
        String path=request.getServletContext().getRealPath("/upload");//虚拟路径
        File dir=new File(path);//把虚拟路径存储到流中，
        dir.mkdirs();//查询是否存在此文件夹，如果存在无操作，如果不存在这里自动创建；
        String srcName=file.getOriginalFilename();//获取流中的文件名称
        File dest=new File(dir,srcName);//spring自动封装的流上传函数
        file.transferTo(dest);//文件上传

        //处理表单业务
        shop.setId(id);
        shop.setCreatename(loginname);
        shop.setCreatetimes(datetimes);
        shop.setPic(srcName);
        shop.setIsdel("0");
        shop.setEm1("");
        shop.setEm2("");
        shop.setEm3("");
        shop.setEm4("");
        shop.setEm5("");
        boolean b = service.save(shop);
        request.setAttribute("b",b);
        return "index/tab_money_shops/save";
    }


    @RequestMapping(value = "tab_money_shops_pre_update")
    public String tab_money_shops_pre_update(String id,HttpServletRequest request){
        TabMoneyShops shop = service.findById(id);
        request.setAttribute("shop",shop);
        return "index/tab_money_shops/update";
    }

    @RequestMapping(value = "tab_money_shops_update")
    public String tab_money_shops_update(TabMoneyShops shop,String ispic,MultipartFile file,HttpServletRequest request) throws IOException {
        if(ispic.equals("0")){
            //不修改pic
            boolean b = service.update_is_not_pic(shop);
            request.setAttribute("b",b);
        }else{
            //修改pic
            //处理流业务
            String path=request.getServletContext().getRealPath("/upload");//虚拟路径
            File dir=new File(path);//把虚拟路径存储到流中，
            dir.mkdirs();//查询是否存在此文件夹，如果存在无操作，如果不存在这里自动创建；
            String srcName=file.getOriginalFilename();//获取流中的文件名称
            File dest=new File(dir,srcName);//spring自动封装的流上传函数
            file.transferTo(dest);//文件上传
            shop.setPic(srcName);

            boolean b = service.update_is_pic(shop);
            request.setAttribute("b",b);
        }
        return "index/tab_money_shops/update";
    }

    @RequestMapping(value = "update_isv_open")
    public String update_isv_open(String id){
        TabMoneyShops shop = new TabMoneyShops();
        shop.setId(id);
        shop.setIsv("1");
        service.update_isv(shop);
        return "redirect:tab_money_shops_view";
    }

    @RequestMapping(value = "update_isv_close")
    public String update_isv_close(String id){
        TabMoneyShops shop = new TabMoneyShops();
        shop.setId(id);
        shop.setIsv("0");
        service.update_isv(shop);
        return "redirect:tab_money_shops_view";
    }

    @RequestMapping(value = "update_flag_open")
    public String update_flag_open(String id){
        TabMoneyShops shop = new TabMoneyShops();
        shop.setId(id);
        shop.setFlag("0");
        service.update_flag(shop);
        return "redirect:tab_money_shops_view";
    }

    @RequestMapping(value = "update_flag_close")
    public String update_flag_close(String id){
        TabMoneyShops shop = new TabMoneyShops();
        shop.setId(id);
        shop.setFlag("1");
        service.update_flag(shop);
        return "redirect:tab_money_shops_view";
    }

    @RequestMapping(value = "update_del")
    public String update_del(String id){
        service.update_isdel(id);
        return "redirect:tab_money_shops_view";
    }


    @RequestMapping(value = "tab_money_shops_view_del")
    public String tab_money_shops_view_del(String msname, String discription, String money, HttpServletRequest request){
        if(null==msname){
            msname = "";
        }
        if(null==discription){
            discription = "";
        }
        if(null==money){
            money = "";
        }
        TabMoneyShops shop = new TabMoneyShops();
        shop.setMsname(msname);
        shop.setDiscription(discription);
        shop.setMoney(money);
        service.searchPages_del(shop,request);
        request.setAttribute("msname",msname);
        request.setAttribute("discription",discription);
        request.setAttribute("money",money);
        return "index/tab_money_shops/view_pre_del";
    }

    @RequestMapping(value = "update_del_rec")
    public String update_del_rec(String id){
        service.update_isdel_rec(id);
        return "redirect:tab_money_shops_view_del";
    }


}
